package com.llb666.e.shop.infrastructure.persistence;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.llb666.e.shop.api.vo.SearchItemsVo;
import com.llb666.e.shop.domain.entity.Items;
import com.llb666.e.shop.domain.entity.bo.ItemCommentBo;
import com.llb666.e.shop.domain.entity.bo.ShopCartBo;
import com.llb666.e.shop.domain.repository.ItemsRepository;
import com.llb666.e.shop.infracore.web.PagedGridResult;
import com.llb666.e.shop.infrastructure.convert.ItemsCvt;
import com.llb666.e.shop.infrastructure.mapper.ItemsCommentsMapper;
import com.llb666.e.shop.infrastructure.mapper.ItemsMapper;
import com.llb666.e.shop.infrastructure.po.ItemsCommentsPo;
import com.llb666.e.shop.infrastructure.po.ItemsPo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

/**
 * @author llb666
 * @date 2022/9/12 22:58
 */
@Repository
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class ItemsRepositoryImpl implements ItemsRepository {

    private final ItemsMapper itemsMapper;
    private final ItemsCommentsMapper itemsCommentsMapper;

    /**
     * 查询商品详情.
     *
     * @param itemId 商品Id
     * @return 商品信息
     */
    @Override
    public Items queryById(String itemId) {
        return ItemsCvt.INSTANCE.po2do(itemsMapper.selectByPrimaryKey(itemId));
    }

    /**
     * 根据商品id和评论等级查询商品评论数
     *
     * @param id    商品id
     * @param level 评论等级
     * @return 商品评论
     */
    @Override
    public Integer getCommentCount(String id, Integer level) {
        ItemsCommentsPo condition = new ItemsCommentsPo();
        condition.setItemId(id);
        if(level != null){
            condition.setCommentLevel(level);
        }
        return itemsCommentsMapper.selectCount(condition);
    }

    /**
     * 查询商品评论.
     *
     * @param paramsMap -
     * @return 商品评论列表
     */
    @Override
    public PagedGridResult<ItemCommentBo> queryItemComments(Map<String, Object> paramsMap, Integer page, Integer size) {
        PageHelper.startPage(page,size);
        List<ItemCommentBo> itemCommentBos = itemsCommentsMapper.queryItemComments(paramsMap);
        PageInfo<ItemCommentBo> itemCommentBoPageInfo = new PageInfo<>(itemCommentBos);
        return PagedGridResult.setterPagedGrid(itemCommentBoPageInfo);
    }

    /**
     * 根据商品规格id列表查询购物车信息.
     *
     * @param itemSpecIds 商品规格id列表
     * @return 购物车明细列表
     */
    @Override
    public List<ShopCartBo> queryItemsBySpecIds(String itemSpecIds) {
        List <String> list = Arrays.asList(itemSpecIds.split(",").clone());
        return itemsMapper.queryItemsBySpecIds(list);
    }

    /**
     * 商品搜索列表查询.
     *
     * @param paramsMap 分类，排序.
     * @return 商品搜索列表
     */
    @Override
    public PagedGridResult<SearchItemsVo> searchItemsByThirdCat(Map<String, Object> paramsMap,Integer page, Integer size) {
        PageHelper.startPage(page,size);
        List<SearchItemsVo> searchItemsVos = itemsMapper.searchItemsByThirdCat(paramsMap);
        PageInfo<SearchItemsVo> searchItemsVoPageInfo = new PageInfo<>(searchItemsVos);
        return PagedGridResult.setterPagedGrid(searchItemsVoPageInfo);
    }

    /**
     * 根据商品id列表查询商品信息.
     *
     * @param itemIds 商品id
     * @return 商品列表
     */
    @Override
    @Async("shopServiceExecutor")
    public CompletableFuture<List<Items>> getItemsFutureByIds(List<String> itemIds) {
        Example example = new Example(ItemsPo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", itemIds);
        List<Items> items = ItemsCvt.INSTANCE.po2doList(itemsMapper.selectByExample(example));
        return CompletableFuture.completedFuture(items);
    }

}
